package com.ruoyi.standingbook.sanitationvehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.sanitationvehicle.domain.SanitationVehicle;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehiclePicBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleFinishedSumVo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportDetailVo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehiclePicVo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleVo;
import com.ruoyi.standingbook.sanitationvehicle.mapper.SanitationVehicleMapper;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 环卫作业车任务完成进度Service业务层处理
 *
 * @author lq
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class SanitationVehicleServiceImpl implements ISanitationVehicleService {

    private final SanitationVehicleMapper baseMapper;

    /**
     * 查询环卫作业车任务完成进度
     */
    @Override
    public SanitationVehicleVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询环卫作业车任务完成进度列表
     */
    @Override
    public TableDataInfo<SanitationVehicleVo> queryPageList(SanitationVehicleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SanitationVehicle> lqw = buildQueryWrapper(bo);
        Page<SanitationVehicleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询环卫作业车任务完成进度列表
     */
    @Override
    public List<SanitationVehicleVo> queryList(SanitationVehicleBo bo) {
        LambdaQueryWrapper<SanitationVehicle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SanitationVehicle> buildQueryWrapper(SanitationVehicleBo bo) {
        LambdaQueryWrapper<SanitationVehicle> lqw = Wrappers.lambdaQuery();
        
		lqw.like(StringUtils.isNotBlank(bo.getProjectName()),
				SanitationVehicle::getProjectName, bo.getProjectName());
		
		lqw.ge(bo.getCheckDateBegin() != null,
				SanitationVehicle::getCheckDate, bo.getCheckDateBegin());
		lqw.le(bo.getCheckDateEnd() != null,
				SanitationVehicle::getCheckDate, bo.getCheckDateEnd());

		lqw.orderByDesc(SanitationVehicle::getCheckDate);
		
        return lqw;
    }

    /**
     * 新增环卫作业车任务完成进度
     */
    @Override
    public Boolean insertByBo(SanitationVehicleBo bo) {
    	SanitationVehicle add = BeanUtil.toBean(bo, SanitationVehicle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改环卫作业车任务完成进度
     */
    @Override
    public Boolean updateByBo(SanitationVehicleBo bo) {
    	SanitationVehicle update = BeanUtil.toBean(bo, SanitationVehicle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SanitationVehicle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除环卫作业车任务完成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertOrUpdate(List<SanitationVehicleImportDetailVo> voList) {
        List<SanitationVehicle> list = BeanCopyUtils.copyList(voList, SanitationVehicle.class);
        list.forEach(e -> {
            e.restBase();
        });
        return this.baseMapper.insertOrUpdateBatch(list);
    }

    @Override
    public List<SanitationVehiclePicVo> getSanitationVehiclePicByDate(String date) {
        List<SanitationVehiclePicBo> parkPicBos = baseMapper.selectPicByDate(date);
        List<SanitationVehiclePicVo> pics = new ArrayList<>();
        parkPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())){
            	SanitationVehiclePicVo sanitationVehiclePicVoPicVo = new SanitationVehiclePicVo();
            	sanitationVehiclePicVoPicVo.setPic(e.getPic1());
                pics.add(sanitationVehiclePicVoPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic2())){
            	SanitationVehiclePicVo sanitationVehiclePicVoPicVo = new SanitationVehiclePicVo();
            	sanitationVehiclePicVoPicVo.setPic(e.getPic2());
                pics.add(sanitationVehiclePicVoPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic3())){
            	SanitationVehiclePicVo sanitationVehiclePicVoPicVo = new SanitationVehiclePicVo();
            	sanitationVehiclePicVoPicVo.setPic(e.getPic3());
                pics.add(sanitationVehiclePicVoPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic4())){
            	SanitationVehiclePicVo sanitationVehiclePicVoPicVo = new SanitationVehiclePicVo();
            	sanitationVehiclePicVoPicVo.setPic(e.getPic4());
                pics.add(sanitationVehiclePicVoPicVo);
            }
        });
        
        return pics;
    }

    @Override
    public BigDecimal getAreaSumByDate(String date) {
    	SanitationVehicleFinishedSumVo parkFinishedSumVo = baseMapper.selectSumByDate(date, null);
        return getAreaSum(parkFinishedSumVo);
    }

    @Override
    public BigDecimal getAreaSumByDateAndCommunity(String date, String community) {
    	SanitationVehicleFinishedSumVo parkFinishedSumVo = baseMapper.selectSumByDate(date, community);
        return getAreaSum(parkFinishedSumVo);
    }

    private BigDecimal getAreaSum(SanitationVehicleFinishedSumVo parkFinishedSumVo) {
        if (null != parkFinishedSumVo){
            BigDecimal greenAreaSum = parkFinishedSumVo.getGreenArea();
            BigDecimal watershedAreaSum = parkFinishedSumVo.getWatershedArea();
            BigDecimal woodlandAreaSum = parkFinishedSumVo.getWoodlandArea();
            return greenAreaSum.add(watershedAreaSum).add(woodlandAreaSum);
        }
        return BigDecimal.ZERO;
    }

    @Override
    public Integer getNumSumByDate(String date) {
        return baseMapper.selectNumSumByDate(date,null , StatusConstant.FINISHED);
    }
}
